<?php
/**
 * 描述
 * Created by Paco
 * Date: 2018/9/11 Time: 18:13
 */

namespace App\Lib\Elasticsearch;


use Elasticsearch\ClientBuilder;

class Elasticsearch
{
    private $_client;
    private $_index = 'default';

    public function __construct($index = null)
    {
        $this->_index = $index ?? env('ES_INDEX', 'default');
        $host = env('ES_HOST', 'http://localhost:9200');

        $hosts = [
            $host,
        ];
        if (!$this->_client) {
            $this->_client = ClientBuilder::create()->setHosts($hosts)->build();
        }


    }

    /**
     *
     * 创建索引
     * @author Paco
     * @createTime 2018/9/14 9:50
     * @param $index
     * @param array $body
     * @param array $query
     * @return array
     */
    public function create($body = [], $query = [])
    {

        print_r($this->_index);
        $params = [
            'index' => $this->_index,
        ];

        if (count($query)) {
            $params = array_merge($params, $query);
        }

        if ($body) {
            $params['body'] = $body;
        }

        return $this->_client->indices()->create($params);
    }


    /**
     *
     * 查询
     * @author Paco
     * @createTime 2018/9/14 9:47
     * @param $body
     * @param array $query
     * @return array
     */
    public function search($body, $query = [])
    {
        $params = [
            'index' => $this->_index,
            'type' => '_doc',
            'body' => $body
        ];
        if ($query) {
            $params = array_merge($params, $query);
        }
        return $this->_client->search($params);
    }

    /**
     * 添加记录（文档）
     * index
     * @author Paco
     * @createTime 2018/7/24 17:04
     * @param $id
     * @param $body
     * @param string $routing
     * @return array
     */
    public function index($body, $query = [])
    {
        $params = [
            'index' => $this->_index,
            'type' => '_doc',
            'body' => $body
        ];

        if ($query) {
            $params = array_merge($params, $query);
        }
        return $this->_client->index($params);
    }

    /**
     *
     * 删除索引
     * @author Paco
     * @createTime 2018/9/14 9:47
     * @param $index
     * @return array
     */
    public function delIndex()
    {
        $params = [
            'index' => $this->_index
        ];
        return $this->_client->indices()->delete($params);

    }

    /**
     *
     * 映射字段
     * @author Paco
     * @createTime 2018/9/14 9:47
     * @param $body
     * @param $query
     * @return array
     */
    public function mapping($body, $query = '')
    {
        $params = [
            'index' => $this->_index,
            'type' => '_doc',
            'body' => $body
        ];

        if ($query) {
            $params = array_merge($params, $query);
        }

        return $this->_client->indices()->putMapping($params);
    }

    /**
     *
     * 查看映射
     * @author Paco
     * @createTime 2018/10/5 17:12
     * @return array
     */
    public function getMapping()
    {
        $params = ['index' => $this->_index];
        return $this->_client->indices()->getMapping($params);
    }

    /**
     *
     * 批量索引（增删改）
     * @author Paco
     * @createTime 2018/9/14 9:48
     * @param array $data
     * @param array $query
     * @return array
     */
    public function bulk($data = [], $query = [])
    {
        $result = [];
        foreach ($data as $k => $item) {
            $params['body'][] = [
                'index' => $query[$k]
            ];
            $params['body'][] = $item;
            if ($k % 100 == 0) {
                $result[] = $this->_client->bulk($params);
                $params = [];
            }
        }
        return $result;
    }

    /**
     *
     * 更新文档
     * @author Paco
     * @createTime 2018/9/14 9:48
     * @param array $body
     * @param array $query
     * @return array
     */
    public function update($body = [], $query = [])
    {
        $params = [
            'index' => $this->_index,
            'type' => '_doc',
            'body' => $body
        ];

        if ($query) {
            $params = array_merge($params, $query);
        }
        return $this->_client->update($params);
    }

    /**
     *
     * 删除文档
     * @author Paco
     * @createTime 2018/9/14 9:49
     * @param $query
     * @return array
     */
    public function delDoc($query)
    {
        $params = [
            'index' => $this->_index,
            'type' => '_doc',
        ];
        if ($query) {
            $params = array_merge($params, $query);
        }
        return $this->_client->delete($params);
    }

}